// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.butler.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.butler.controller.request.QueryCommentReq;
import com.liuxinlong.modules.butler.service.CommentService;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarCommentReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarStatusReq;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-12-07
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 分页查询评论信息
     *
     * @param req 请求信息
     * @return 评论信息列表
     */
    @PostMapping("page")
    public BaseResp pageCommentInfo(@RequestBody QueryCommentReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("keyword", req.getKeyword());
        queryMap.put("status", req.getStatus());
        List<Map<String, Object>> dataList = commentService.pageCommentManageList(queryMap);
        int totalNum = commentService.getCommentManageCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询评论信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }
    /**
     * 评论置顶
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("topping")
    @LogAnnotation(value = "置顶评论", business = LogBusinessConstants.COMMENT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp toppingComment(@RequestBody BaseDeleteReq req) {
        commentService.toppingComment(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("置顶评论成功");
        return resp;
    }

    /**
     * 修改评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改评论信息", business = LogBusinessConstants.COMMENT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateComment(@RequestBody UpdateBarCommentReq req) {
        BaseResp resp = new BaseResp();
        Comment comment = new Comment();
        comment.setId(req.getId());
        comment.setContent(req.getContent());
        comment.setAttach(req.getAttach());
        commentService.updateComment(comment);
        resp.setRetMessage("修改评论信息成功");
        return resp;
    }

    /**
     * 删除评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除评论信息", business = LogBusinessConstants.COMMENT_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteComment(@RequestBody BaseDeleteReq req) {
        commentService.deleteComment(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除评论信息成功");
        return resp;
    }

    /**
     * 审核评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("examine")
    @LogAnnotation(value = "审核评论信息", business = LogBusinessConstants.COMMENT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp examineComment(@RequestBody UpdateBarStatusReq req) {
        commentService.examineComment(req.getId(), req.getResult());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("审核评论信息成功");
        return resp;
    }


    /**
     * 导出评论信息
     *
     * @param req 请求信息
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出评论信息", business = LogBusinessConstants.COMMENT_MANAGE,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportComment(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        commentService.exportComment(req.getId(), response);
    }

}
